layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	var table_filter = 'returned_list',
		where_json = {
			_o: 'sort',
			_b: 'asc'
		},
		form_filter = 'returned_search_form';
	// 渲染表格
	if (load_data === true) {
		table.render({
			elem: '#' + table_filter,
			id: table_filter,
			url: returned_list_url,
			method: 'post',
			height: 'full-200',
			where: {
				_o: 'sort',
				_b: 'asc'
			},
			cols: [returned_cols],
			page: {
				layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
				limit: 30,
				theme: '#393d49',
				groups: 8
			},
			autoSort: false,
			initSort: {
				field: 'returned_id',
				type: 'asc'
			}
		});
	}
	// 编辑表格
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var returned_id = data.returned_id;
		switch (e) {
			case 'refuse':
				// 拒绝申请
				layer.confirm('确定拒绝退换货吗？', function(e) {
					var data_back = {
						returned_id: returned_id,
						returned_state: 3
					};
					send_info(returned_save_url, data_back, function(data) {
						table.reload(table_filter);
						layer.msg('操作成功', {
							icon: 1
						});
						if(site_id == "115"){
							// 北京政采
							layer.msg('保存成功，准备推送数据', {
								icon: 1
							});
							layer.open({
								type: 2,
								anim: -1,
								title: !1,
								closeBtn: !1,
								offset: 'r',
								shade: 0.1,
								shadeClose: !0,
								area: '400px',
								skin: "layui-anim layui-anim-rl layui-layer-adminRight",
								content: push_beijing_url + '?returned_id=' + returned_id
							})
						}
					})
					
				})
				break;
			case 'over':
				// 通过申请
				layer.confirm('确定完成退换货吗？', function(e) {
					var data_back = {
						returned_id: returned_id,
						returned_state: 2
					};
					send_info(returned_save_url, data_back, function(data) {
						table.reload(table_filter);
						layer.msg('操作成功', {
							icon: 1
						});
						if(site_id == "115"){
							// 北京政采
							layer.msg('保存成功，准备推送数据', {
								icon: 1
							});
							layer.open({
								type: 2,
								anim: -1,
								title: !1,
								closeBtn: !1,
								offset: 'r',
								shade: 0.1,
								shadeClose: !0,
								area: '400px',
								skin: "layui-anim layui-anim-rl layui-layer-adminRight",
								content: push_beijing_over_url + '?returned_id=' + returned_id
							})
						}
					})
				})
				break;
			case 'pass':
				// 通过申请
				layer.confirm('确定同意退换货吗？', function(e) {
					var data_back = {
						returned_id: returned_id,
						returned_state: 3
					};
					send_info(returned_save_url, data_back, function(data) {
						table.reload(table_filter);
						layer.msg('操作成功', {
							icon: 1
						});
						if(site_id == "115"){
							// 北京政采
							layer.msg('保存成功，准备推送数据', {
								icon: 1
							});
							layer.open({
								type: 2,
								anim: -1,
								title: !1,
								closeBtn: !1,
								offset: 'r',
								shade: 0.1,
								shadeClose: !0,
								area: '400px',
								skin: "layui-anim layui-anim-rl layui-layer-adminRight",
								content: push_beijing_url + '?returned_id=' + returned_id
							})
						}
					})
				})
				break;
			case 'push_send_back':
				// 获取仓库
				var depot_config = {
					_o: 'depot_id',
					_b: 'asc',
					page: 1,
					limit: 50
				};
				send_info(depot_list_url, depot_config, function(data) {
					var depot_ul = $('form[lay-filter="tpl_express_back"]').find('select[lay-filter="depot"]');
					$('form[lay-filter="tpl_express_back"]').find('input[name="returned_id"]').val(returned_id);
					$('form[lay-filter="tpl_express_back"]').find('input[name="express_no"]').val(data.count);
					depot_ul.html('');
					for (var i in data.data) {
						var e = data.data[i];
						var op = '<option value="' + e.depot_id + '">' + e.depot_name + '</option>';
						depot_ul.append(op);
					}
					form.render('select');
				})
				layer.open({
					content: $('#returned_express'),
					type: 1,
					anim: 2,
					title: '产品发货',
					closeBtn: true,
					shadeClose: true,
					area: ['650px', '510px'],
					shade: 0.1
				})
				break;
			case 'get_send_back':
				layer.confirm('确定已收到回寄产品吗？', function(index) {
					var data_back = {
						returned_id: returned_id,
						is_send_back: 3
					};
					send_info(returned_save_url, data_back, function(data) {
						table.reload(table_filter);
						layer.msg('产品已收货', {
							icon: 1
						});
					})
				});
				break;
			case 'send_back_goods':
				$('#returned_form_box').find('input[name="returned_id"]').val(returned_id);
				layer.open({
					content: $('#returned_form_box'),
					type: 1,
					anim: 2,
					title: '回寄产品',
					closeBtn: true,
					shadeClose: true,
					area: ['650px', '460px'],
					shade: 0.1
				})
				break;
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {
					send_info(order_delete_url, {
						order_id: order_id
					}, function(data) {
						if (data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if ($('table[lay-filter="recycle_order_list"]').length > 0) {
								table.reload('recycle_order_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			default:
				break;
		}
	})
	// 确认发货
	form.on('submit(back_submit_express)', function(data) {
		var field = data.field;
		field.shopping_state = 2;
		field.is_send_back = 3;
		send_info(returned_save_url, field, function(data) {
			layer.closeAll('page');
			layer.msg('产品发货成功', {
				icon: 1,
				anim: 6
			});
			table.reload(table_filter);
			if(site_id == "115"){
				// 北京政采
				layer.msg('保存成功，准备推送数据', {
					icon: 1
				});
				layer.open({
					type: 2,
					anim: -1,
					title: !1,
					closeBtn: !1,
					offset: 'r',
					shade: 0.1,
					shadeClose: !0,
					area: '400px',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: push_beijing_over_url + '?returned_id=' + field.returned_id
				})
			}
		})
	})
	// 确认回寄
	form.on('submit(submit_returned_back)', function(data) {
		var field = data.field;
		if (field.back_express == '') {
			layer.msg('请选择物流公司', {
				icon: 2,
				anim: 6
			});
			return false;
		}
		if (field.back_express_no == '') {
			layer.msg('请填写回寄单号', {
				icon: 2,
				anim: 6
			});
			return false;
		}
		field.is_send_back = 2;
		send_info(returned_save_url, field, function(data) {
			layer.closeAll('page');
			layer.msg('产品回寄成功', {
				icon: 2,
				anim: 6
			});
			table.reload(table_filter);
		})
	})

	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})
	var select_search = function(field, value) {
		var where = {};
		where[field] = value;
		where_json[field] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	};
	form.on('radio', function(data) {
		var value = data.value;
		var e = data.elem;
		var name = $(e).attr('name');
		select_search(name, value);
	});
})
